package com.yannan.lock.core.util;


import org.apache.commons.beanutils.PropertyUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.util.Assert;

import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.util.*;

public class ModelUtil {
    private static Logger log = Logger.getLogger(ModelUtil.class);

    private ModelUtil() {
    }



    public static void copyProps(Object source, Object target, String... ignorProps) {
        if(source instanceof Map) {
            try {
                Map e = (Map)source;
                HashSet ignorPropsSet = new HashSet();
                if(ignorProps != null) {
                    String[] targetMap = ignorProps;
                    int value = ignorProps.length;

                    for(int var6 = 0; var6 < value; ++var6) {
                        String key = targetMap[var6];
                        ignorPropsSet.add(key);
                    }
                }

                Iterator var11 = e.keySet().iterator();

                while(var11.hasNext()) {
                    Object var10 = var11.next();
                    if(var10 != null && !ignorPropsSet.contains(var10)) {
                        Object var12 = e.get(var10);
                        if(target instanceof Map) {
                            Map var13 = (Map)target;
                            var13.put(var10, var12);
                        } else if(PropertyUtils.isWriteable(target, var10.toString())) {
                            PropertyUtils.setProperty(target, var10.toString(), var12);
                        }
                    }
                }
            } catch (Exception var9) {
                throw new RuntimeException(var9);
            }
        } else {
            BeanUtils.copyProperties(source, target, ignorProps);
        }

    }

    public static void copyPropsInc(Object source, Object target, String... incProps) {
        try {
            HashSet e = new HashSet();
            int var5;
            int var6;
            if(incProps != null) {
                String[] var7 = incProps;
                var6 = incProps.length;

                for(var5 = 0; var5 < var6; ++var5) {
                    String pd = var7[var5];
                    e.add(pd);
                }
            }

            PropertyDescriptor[] var12;
            var6 = (var12 = PropertyUtils.getPropertyDescriptors(source)).length;

            for(var5 = 0; var5 < var6; ++var5) {
                PropertyDescriptor var11 = var12[var5];
                String key = var11.getName();
                if(key != null && e.contains(key)) {
                    Object value = PropertyUtils.getProperty(source, key);
                    if(PropertyUtils.isWriteable(target, key.toString())) {
                        PropertyUtils.setProperty(target, key.toString(), value);
                    }
                }
            }

        } catch (Exception var10) {
            throw new RuntimeException(var10);
        }
    }



    public static Map<String, Object> convBeanToMap(Object bean, String... propNames) {
        Assert.notNull(propNames, "请指定属性名称");
        HashMap rtn = new HashMap();
        int var5;
        if(propNames.length != 0 && !"*".equals(propNames[0])) {
            String[] var15 = propNames;
            var5 = propNames.length;

            for(int var14 = 0; var14 < var5; ++var14) {
                String var13 = var15[var14];
                Object var16 = null;

                try {
                    if(PropertyUtils.isReadable(bean, var13)) {
                        var16 = PropertyUtils.getProperty(bean, var13);
                    }
                } catch (Exception var11) {
                    log.debug("获取其它属性时出错：" + var11.getMessage());
                }

                rtn.put(var13, var16);
            }
        } else {
            PropertyDescriptor[] propName = PropertyUtils.getPropertyDescriptors(bean);
            PropertyDescriptor[] propValue = propName;
            int var6 = propName.length;

            for(var5 = 0; var5 < var6; ++var5) {
                PropertyDescriptor propDesc = propValue[var5];
                String e = propDesc.getName();
                Object propValue1 = null;

                try {
                    propValue1 = PropertyUtils.getProperty(bean, e);
                } catch (Exception var12) {
                    log.debug("获取其它属性时出错：" + var12.getMessage());
                }

                rtn.put(e, propValue1);
            }
        }

        return rtn;
    }

    public static Object getPropertyValue(Object obj, String propName) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
        return PropertyUtils.getProperty(obj, propName);
    }

    public static Object removeNullRelation(Object obj, String relationIdName) throws Exception {
        int idx = relationIdName.lastIndexOf(".");
        Assert.isTrue(idx > 0, "关系id属性名无效：" + relationIdName);
        String relationName = relationIdName.substring(0, idx);
        Object relationId = PropertyUtils.getProperty(obj, relationIdName);
        if(relationId instanceof String) {
            String id = (String)relationId;
            if(id == null || id.trim().length() == 0) {
                PropertyUtils.setProperty(obj, relationName, (Object)null);
            }
        } else if(relationId == null) {
            PropertyUtils.setProperty(obj, relationName, (Object)null);
        }

        return obj;
    }

    public static List treeToList(Object model, String subPropName, List list) throws Exception {
        Collection subs = (Collection)PropertyUtils.getProperty(model, subPropName);
        if(subs != null && subs.size() > 0) {
            Iterator var5 = subs.iterator();

            while(var5.hasNext()) {
                Object sub = var5.next();
                treeToList(sub, subPropName, list);
            }
        }

        list.add(model);
        return list;
    }

    public static void trimFields(Object model, String... propNames) throws Exception {
        Assert.notEmpty(propNames, "请指定要去前后空格的属性名");
        String[] var5 = propNames;
        int var4 = propNames.length;

        for(int var3 = 0; var3 < var4; ++var3) {
            String propName = var5[var3];
            Object val = PropertyUtils.getProperty(model, propName);
            if(val instanceof String && val != null) {
                String valStr = (String)val;
                valStr = valStr.trim();
                PropertyUtils.setProperty(model, propName, valStr);
            }
        }

    }

    
}